🔶章節:
🔹[開頭]
🔹[管理者 VS 使用者]
🔹[實作的資料表內容]
🔹[目前的文件]
🔹[網站管理畫面]
🔹[管理新增預約]
🔹[完成標示]
🔹[網站實作呈現]
🔹[總結]
如果影片中不清楚,需要補充的地方我會再添加到這邊~
👆教學中的[練習]程式碼一併附上,影片中會有每組的講解、說明更清楚👆
adminWeb.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>管理者系統</title>
</head>
<body>
<?php
session_start();
include 'db_connect.php';
// 檢查是否為管理者
if (isset($_SESSION['user_id']) && $_SESSION['user_id'] == 1) {
$action = isset($_GET['action']) ? $_GET['action'] : 'show_today';
// 顯示連結
echo '<h2>管理者頁面</h2>';
echo '<a href="adminWeb.php?action=show_all">顯示所有訂單</a> | ';
echo '<a href="adminWeb.php?action=show_today">顯示今天的預約</a> | ';
echo '<a href="createWeb.php">建立訂單</a><br><br>';
if ($action === 'show_today') {
$currentDate = date('Y-m-d');
$sql = "SELECT order_id, type, date, time, name, phone, created_at, status
FROM appointments
WHERE date = '$currentDate'
ORDER BY status DESC, time ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<div>";
echo "<p>訂單編號:" . $row['order_id'] . "</p>";
echo "<p>預約類型:" . $row['type'] . "</p>";
echo "<p>預約日期:" . $row['date'] . "</p>";
echo "<p>預約時間:" . $row['time'] . "</p>";
echo "<p>預約姓名:" . $row['name'] . "</p>";
echo "<p>預約電話:" . $row['phone'] . "</p>";
echo "<p>建立:" . $row['created_at'] . "</p>";
echo "<p>狀態:" . $row['status'] . "</p>";
if ($row['status'] === '未完成') {
echo "<form action='cancel_reservation.php' method='post'>";
echo "<input type='hidden' name='order_id' value='" . $row['order_id'] . "'>";
echo "<button type='submit'>取消預約</button>";
echo "</form>";
// 添加已完成按鈕功能
echo "<form method='post' action='mark_completed.php'>";
echo "<input type='hidden' name='order_id' value='" . $row['order_id'] . "'>";
echo "<button type='submit'>完成訂單</button>";
echo "</form>";
} else {
echo "<p>已完成訂單,無法執行操作。</p>";
}
echo "</div>";
echo "<hr>";
}
} else {
echo "<p>沒有任何預約項目</p>";
}
}elseif ($action === 'show_all') {
$sql = "SELECT order_id, type, date, time, name, phone, created_at, status FROM appointments ORDER BY date DESC, time DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<div>";
echo "<p>訂單編號:" . $row['order_id'] . "</p>";
echo "<p>預約類型:" . $row['type'] . "</p>";
echo "<p>預約日期:" . $row['date'] . "</p>";
echo "<p>預約時間:" . $row['time'] . "</p>";
echo "<p>預約姓名:" . $row['name'] . "</p>";
echo "<p>預約電話:" . $row['phone'] . "</p>";
echo "<p>建立:" . $row['created_at'] . "</p>";
echo "<p>狀態:" . $row['status'] . "</p>";
if ($row['status'] === '未完成') {
echo "<form action='cancel_reservation.php' method='post'>";
echo "<input type='hidden' name='order_id' value='" . $row['order_id'] . "'>";
echo "<button type='submit'>取消預約</button>";
echo "</form>";
// 添加已完成按鈕功能
echo "<form method='post' action='mark_completed.php'>";
echo "<input type='hidden' name='order_id' value='" . $row['order_id'] . "'>";
echo "<button type='submit'>完成訂單</button>";
echo "</form>";
} else {
echo "<p>已完成訂單,無法執行操作。</p>";
}
echo "</div>";
echo "<hr>";
}
} else {
echo "<p>沒有任何預約項目</p>";
}
}
} else {
echo "只有管理者可以訪問此頁面";
}
$conn->close();
?>
</body>
</html>
createWeb.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>建立訂單</title>
</head>
<body>
<?php
session_start();
include 'db_connect.php';
if (isset($_SESSION['user_id']) && $_SESSION['user_id'] == 1) {
echo '<h2>建立訂單</h2>';
echo '<a href="adminWeb.php?action=show_all">顯示所有訂單</a> | ';
echo '<a href="adminWeb.php?action=show_today">顯示今天的預約</a> | ';
echo '<a href="create_order.php">建立訂單</a><br><br>';
echo '<h3>填寫預約資訊</h3>';
echo '<form action="create_order.php" method="post">';
echo '預約類型: ';
echo '<select name="type">';
echo '<option value="VR體驗">VR體驗</option>';
echo '<option value="諮商師諮詢">諮商師諮詢</option>';
echo '</select><br>';
echo '預約日期:<input type="date" name="date" required><br>';
echo '預約時間:<input type="time" name="time" required><br>';
echo '姓名:<input type="text" name="name" required><br>';
echo '郵件地址:<input type="text" name="email" ><br>';
echo '電話:<input type="text" name="phone" required><br>';
echo '<input type="submit" value="建立訂單">';
echo '</form>';
} else {
echo "只有管理者可以訪問此頁面";
}
$conn->close();
?>
</body>
</html>
create_order.php
<?php
session_start();
include 'db_connect.php';
include 'user_info.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_SESSION['user_id'])) {
$user_id = $_SESSION['user_id'];
$userInfo = getUserInfo($user_id, $conn);
$account = $userInfo['account'];
$type = $_POST['type'];
$date = $_POST['date'];
$time = $_POST['time'];
$email = $_POST['email'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$created_at = date('Y-m-d H:i:s');
//資料存入 資料庫 'appointments' 資料表
$sql = "INSERT INTO appointments (account, type, date, time, email, name, phone, status)
VALUES ('$account', '$type', '$date', '$time', '$email', '$name', '$phone', '未完成')";
if ($conn->query($sql) === TRUE) {
echo "訂單建立成功!";
echo "<br><a href='adminWeb.php'>回到管理者畫面</a>";
} else {
echo "錯誤:" . $sql . "<br>" . $conn->error;
}
}
}
$conn->close();
?>
mark_completed.php
<?php
include 'db_connect.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['order_id'])) {
$orderId = $_POST['order_id'];
// 更新訂單狀態為已完成
$updateQuery = "UPDATE appointments SET status = '已完成' WHERE order_id = $orderId";
mysqli_query($conn, $updateQuery);
// 重新導向回管理者頁面
header("Location: adminWeb.php?action=show_today");
exit;
}
?>
從網頁開發到資料庫管理,從登入驗證到訂單管理。透過這些學習,您已經建立了一個完整的預約系統,能夠讓使用者進行預約、管理者查看訂單、以及進行狀態的更改。這個過程不僅加深了對網頁開發和資料庫操作的理解,也培養了解決問題和設計系統的能力。
我們在下一部影片將會檢視及整理我們的網頁! 明天見~~